package com.ruoyi.dispatch.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.dispatch.domain.MonthSalesReport;
import com.ruoyi.dispatch.enums.SalesDayReportCol;
import com.ruoyi.dispatch.vo.SalesDayReportVO;
import com.ruoyi.framework.utils.BeanCopyUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dispatch.domain.SalesDayReport;
import com.ruoyi.dispatch.service.ISalesDayReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 *  销售日报 Controller
 * 
 * @author wuxiaofang
 * @date 2025-04-11
 */
@RestController
@RequestMapping("/dispatch/salesDayReport")
@Anonymous
public class SalesDayReportController extends BaseController
{
    @Autowired
    private ISalesDayReportService salesDayReportService;

    /**
     * 查询 销售日报 列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:SalesDayReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(SalesDayReport salesDayReport,String[] columnArray)
    {
        startPage();
        List<SalesDayReport> list = salesDayReportService.selectSalesDayReportList(salesDayReport);
        //组装展示列名信息
        List<Map<String,String>> columns = null;
        if(columnArray!=null && columnArray.length!=0){
            columns = salesDayReportService.handleColumnMap(columnArray);
        }
        TableDataInfo tableData =  getDataTable(list);
        tableData.setColumns(columns);

        return tableData;
    }

    /**
     * 导出 销售日报 列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:SalesDayReport:export')")
    @Log(title = " 销售日报 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalesDayReport salesDayReport)
    {
        List<SalesDayReport> list = salesDayReportService.selectSalesDayReportList(salesDayReport);
        ExcelUtil<SalesDayReport> util = new ExcelUtil<SalesDayReport>(SalesDayReport.class);
        util.exportExcel(response, list, " 销售日报 数据");
    }

    /**
     * 获取 销售日报 详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:SalesDayReport:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId)
    {
        return success(salesDayReportService.selectSalesDayReportByReportId(reportId));
    }

    /**
     * 新增 销售日报 
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:SalesDayReport:add')")
    @Log(title = " 销售日报 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalesDayReport salesDayReport)
    {
        //插入数据
        int insertResult = salesDayReportService.insertSalesDayReport(salesDayReport);
        //查询此次新增的记录
        SalesDayReport report = salesDayReportService.selectSalesDayReportByReportId(salesDayReport.getReportId());
        SalesDayReportVO vo = BeanCopyUtils.copyProperties(report,SalesDayReportVO.class);
        //组装默认展示的列信息
        vo.setDisplayCols(SalesDayReportCol.getDefalutCols());

        return toAjax(insertResult,vo);
    }

    /**
     * 修改 销售日报 
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:SalesDayReport:edit')")
    @Log(title = " 销售日报 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalesDayReport salesDayReport)
    {
        return toAjax(salesDayReportService.updateSalesDayReport(salesDayReport));
    }

    /**
     * 删除 销售日报 
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:SalesDayReport:remove')")
    @Log(title = " 销售日报 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(salesDayReportService.deleteSalesDayReportByReportIds(reportIds));
    }
    @RequestMapping("countSalesReport")
    public List<MonthSalesReport> countSalesReport(SalesDayReport salesDayReport){
        List<MonthSalesReport> monthSalesReports = salesDayReportService.countSalesReport(salesDayReport);
        return monthSalesReports;
    }
}
